package com.applet.applet_shangcheng.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.applet.applet_shangcheng.dao.GoodsMapper;
import com.applet.applet_shangcheng.entity.Goods;
import com.applet.applet_shangcheng.entity.GoodsClass;
import com.applet.applet_shangcheng.entity.ShufflingFigure;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jdk.nashorn.internal.parser.JSONParser;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ImageService imageService;

    public Map<String, Object> findAll() {
        List<Goods> list = goodsMapper.findAll();
        Map<String, Object> map = new HashMap<>();
        for (Goods goods : list) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("goodsId", goods.getGoodsId());
            goodsMap.put("goodsName", goods.getGoodsName());
            goodsMap.put("goodsIntroduce", goods.getGoodsIntroduce());
            goodsMap.put("image", goods.getImage());
            goodsMap.put("price", goods.getPrice());
            map.put(goods.getGoodsName(), goodsMap);
        }
        return map;
    }

    ;

    public Map<String, Object> findAllShelves() {
        List<Goods> list = goodsMapper.findAllShelves();
        Map<String, Object> map = new HashMap<>();
        for (Goods goods : list) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("goodsId", goods.getGoodsId());
            goodsMap.put("goodsName", goods.getGoodsName());
            goodsMap.put("goodsIntroduce", goods.getGoodsIntroduce());
            goodsMap.put("image", goods.getImage());
            goodsMap.put("price", goods.getPrice());
            map.put(goods.getGoodsName(), goodsMap);
        }
        return map;
    }

    ;

    public Goods findById(int goodsId) {
        Goods goods = goodsMapper.findById(goodsId);
        String images = goods.getImages();
        if (images != null) {
            JSONArray objects = JSON.parseArray(images);
            List<String> list = new ArrayList<>();
            if (goods.getImage() != null) {
                list.add(goods.getImage());
            }
            for (Object object : objects) {
                list.add(object.toString());
            }
            goods.setImagess(list);
        }
        GoodsClass byGoodsId = goodsMapper.findByGoodsId(goodsId);
        if (byGoodsId!=null){
            goods.setSocial(byGoodsId.getSocial());
            goods.setStudents(byGoodsId.getStudents());
            goods.setSoldier(byGoodsId.getSoldier());
        }
        return goods;
    }

    ;

    public Goods findGoods(int goodsId) {
        Goods goods = goodsMapper.findGoods(goodsId);
        return goods;
    }

    ;
    @Transactional(rollbackFor = Exception.class)
    public int insertGoods(Goods goods) {
        goods.setCreateTime(new Date());
        int result = goodsMapper.insertGoods(goods);
        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGoodsId(goods.getGoodsId());
        goodsClass.setSocial(goods.getSocial());//社会人士
        goodsClass.setStudents(goods.getStudents());//学生
        goodsClass.setSoldier(goods.getSoldier());//军人
        goodsClass.setStatus(goods.getStatus());
        int i = goodsMapper.insertGoodsClass(goodsClass);
        return result;
    }

    ;

    @Transactional(rollbackFor = Exception.class)
    public int updateGoods(Goods goods) {
        Goods goods1 = goodsMapper.findGoods(goods.getGoodsId());
        imageService.updateImageStatus(goods1.getImage());
        goods.setUpdateTime(new Date());
        GoodsClass goodsClass = new GoodsClass();
        goodsClass.setGoodsId(goods.getGoodsId());
        goodsClass.setSocial(goods.getSocial());//社会人士
        goodsClass.setStudents(goods.getStudents());//学生
        goodsClass.setSoldier(goods.getSoldier());//军人
        goodsClass.setStatus(goods.getStatus());
        int i = goodsMapper.updateGoodsClass(goodsClass);
        int result = goodsMapper.updateGoods(goods);
        if (result > 0) {
            return result;
        }
        return 0;
    }

    ;

    public int updateStatus(int id, int status) {
        int result = goodsMapper.updateStatus(id, status);
        return result;
    }

    ;
    @Transactional(rollbackFor = Exception.class)
    public int deleteGoods(int id) {
        Goods goods = goodsMapper.findId(id);
        int i = goodsMapper.deleteGoodsClass(goods.getGoodsId());
        int result = goodsMapper.deleteGoods(id);
        return result;
    }

    public PageResult findAlls(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<ShufflingFigure> page = goodsMapper.findAlls(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(), page.getResult());
    }
    public Goods findByGoodsName(String goodsName) {
        return goodsMapper.findByGoodsName(goodsName);
    }

    public Goods findId(int id) {
        return goodsMapper.findId(id);
    }

    public int updateNumber(int goodsId, int number) {
        return goodsMapper.updateNumber(goodsId, number);
    }

    public int insertGoodsClass(GoodsClass goodsClass) {
        return goodsMapper.insertGoodsClass(goodsClass);
    }

    ;

    public GoodsClass findByGoodsId(int goodsId) {
        return goodsMapper.findByGoodsId(goodsId);
    }

    ;

    public int updateGoodsClass(GoodsClass goodsClass) {
        return goodsMapper.updateGoodsClass(goodsClass);
    }

    ;

    public int deleteGoodsClass(int goodsId) {
        return goodsMapper.deleteGoodsClass(goodsId);
    }

}
